﻿namespace ReplayDataFile
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;

    public class BlockDataReader : BlockDataBase
    {
        public BlockDataReader(string filePath) : base(filePath)
        {
            this.IsUnZipRecord = true;
        }

        protected List<BlockFile> GetBlockFileList(DateTime beginTime, DateTime endTime)
        {
            List<BlockFile> list = new List<BlockFile>();
            foreach (BlockFile file in base.FileList)
            {
                if (file.IsContainData(beginTime, endTime))
                {
                    list.Add(file);
                }
            }
            return list;
        }

        private DataRecord InsertExitDataRecord(DataRecord rec, DataRecord preRec)
        {
            if ((preRec != null) && ((rec.RecType == DataRecordType.StartupRecord) && (preRec.RecType != DataRecordType.ExitRecord)))
            {
                return new DataRecord { RecType = DataRecordType.ExitRecord, IsInterpolationRecord = true, Time = preRec.Time, RecordNo = preRec.RecordNo, DataType = preRec.DataType };
            }
            return null;
        }

        public DataRecord Read(RecordIndex recIndex)
        {
            if (recIndex != null)
            {
                foreach (BlockFile file in base.FileList)
                {
                    if (file.Header.FileId == recIndex.FileId)
                    {
                        BlockFileReader reader = new BlockFileReader(file.FileName);
                        if (reader.Open())
                        {
                            reader.IsUnzipRecord = this.IsUnZipRecord;
                            DataRecord record = reader.Read(recIndex);
                            reader.Close();
                            return record;
                        }
                    }
                }
            }
            return null;
        }

        public ICollection<DataRecord> Read(DateTime beginTime, DateTime endTime)
        {
            return this.Read((List<int>) null, beginTime, endTime);
        }

        public ICollection<DataRecord> Read(List<int> dataTypeList, DateTime beginTime, DateTime endTime)
        {
            return this.ReadAct(dataTypeList, beginTime, endTime);
        }

        public ICollection<DataRecord> Read(int dataType, DateTime beginTime, DateTime endTime)
        {
            List<int> dataTypeList = new List<int> {
                dataType
            };
            return this.Read(dataTypeList, beginTime, endTime);
        }

        public ICollection<DataRecord> Read(List<string> dataTypeNameList, DateTime beginTime, DateTime endTime)
        {
            return this.ReadAct(dataTypeNameList, beginTime, endTime);
        }

        public ICollection<DataRecord> Read(string dataTypeName, DateTime beginTime, DateTime endTime)
        {
            List<string> dataTypeNameList = new List<string> {
                dataTypeName
            };
            return this.Read(dataTypeNameList, beginTime, endTime);
        }

        private ICollection<DataRecord> ReadAct(object dataTypes, DateTime beginTime, DateTime endTime)
        {
            List<BlockFile> blockFileList = this.GetBlockFileList(beginTime, endTime);
            List<DataRecord> list2 = new List<DataRecord>();
            Dictionary<int, DataRecord> dictionary = new Dictionary<int, DataRecord>();
            foreach (BlockFile file in blockFileList)
            {
                BlockFileReader reader = new BlockFileReader(file.FileName);
                if (reader.Open())
                {
                    reader.IsUnzipRecord = this.IsUnZipRecord;
                    ICollection<DataRecord> collection = null;
                    if (dataTypes != null)
                    {
                        if (dataTypes is List<int>)
                        {
                            collection = reader.Read(dataTypes as List<int>, beginTime, endTime);
                        }
                        else if (dataTypes is List<string>)
                        {
                            collection = reader.Read(dataTypes as List<string>, beginTime, endTime);
                        }
                        else
                        {
                            Debug.Assert(false, "不支持的参数类型！");
                        }
                    }
                    else
                    {
                        collection = reader.Read(beginTime, endTime);
                    }
                    reader.Close();
                    if (this.IsInsertInterpolationRecord)
                    {
                        foreach (DataRecord record in collection)
                        {
                            DataRecord preRec = null;
                            if (dictionary.ContainsKey(record.DataType))
                            {
                                preRec = dictionary[record.DataType];
                            }
                            DataRecord item = this.InsertExitDataRecord(record, preRec);
                            if (item != null)
                            {
                                list2.Add(item);
                            }
                            list2.Add(record);
                            dictionary[record.DataType] = record;
                        }
                    }
                    else
                    {
                        list2.AddRange(collection);
                    }
                }
            }
            return list2;
        }

        public List<RecordIndex> ReadIndex(int dataType, DateTime beginTime, DateTime endTime)
        {
            List<BlockFile> blockFileList = this.GetBlockFileList(beginTime, endTime);
            List<RecordIndex> list2 = new List<RecordIndex>();
            foreach (BlockFile file in blockFileList)
            {
                BlockFileReader reader = new BlockFileReader(file.FileName);
                if (reader.Open())
                {
                    reader.IsUnzipRecord = this.IsUnZipRecord;
                    List<RecordIndex> collection = reader.ReadIndex(dataType, beginTime, endTime);
                    reader.Close();
                    list2.AddRange(collection);
                }
            }
            return list2;
        }

        public bool IsInsertInterpolationRecord { get; set; }

        public bool IsUnZipRecord { get; set; }
    }
}

